//
//  SocialDelegate.m
//  nonghubao
//
//  Created by Fenrir on 13-12-13.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "SocialDelegate.h"

@implementation SocialDelegate
@synthesize dynamic = _dynamic;

- (id)initWithDynamic:(NSDictionary *)dynamic
{
    self = [super init];
    if (self) {
        _dynamic = dynamic;
    }
    return self;
}

//处理不同的平台
//如果需要分开出来各个平台的分享，尽量就要针对每一个平台做处理，避免照成图片的重复分享（即分享的图片为上一次分享的图片）。

//注意事项：如果分享时，某些属性不需要分享,例如shareImage，那么确保它被置为nil，以免造成以前的信息被分享出去。（感觉应该是友盟设计的问题，在分享后应该清理缓存）

-(void)didSelectSocialPlatform:(NSString *)platformName withSocialData:(UMSocialData *)socialData
{
    
    //分享链接
    NSString *url = [NSString stringWithFormat:_SYS_SHARE_PAGE,[_CONFIG_APP_KEY lowercaseString],[_dynamic[@"id"] integerValue]];
    
    //动态类型
    NSInteger type = [_dynamic[@"type"] integerValue];
    
    if (type != DynamicCategoryMood) {
    
        NSString *productName = _dynamic[@"product"][@"name"];
        NSString *productDescription = _dynamic[@"product"][@"description"];
        
        //商品图片
        NSArray *pictures = (NSArray *)_dynamic[@"product"][@"pictures"];

        if (pictures != nil && pictures.count > 0) {
            socialData.shareImage = [Helper imageSuitable:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:pictures[0][@"previewUrl"]]]]]; //心情图片
        }
        else {
            socialData.shareImage = nil;
            
        }

        //分别配置各个平台的分享内容
        if ([platformName isEqualToString:UMShareToSina] || [platformName isEqualToString:UMShareToTencent] || [platformName isEqualToString:UMShareToQzone])
        {
            //分享到新浪微博和腾讯微博 文本+图片
            //控制分享到微博的文本字数
            NSString *weiboShareText = nil;
            
            if ([[NSString stringWithFormat:@"%@ %@\n%@",productName, url, productDescription] length] > 140) {
                weiboShareText = [[NSString stringWithFormat:@"%@ %@\n%@",productName, url, productDescription] substringWithRange:NSMakeRange(0, 139)];
            }
            else{
                weiboShareText = [NSString stringWithFormat:@"%@ %@\n%@",productName, url, productDescription];
            }
            socialData.shareText = weiboShareText;
        }

        //分享到微信
        else if ([platformName isEqualToString:UMShareToWechatSession] || [platformName isEqualToString:UMShareToWechatTimeline])
        {
            //分享内嵌文字
            socialData.shareText = productDescription;
            
            //分享标题
            socialData.extConfig.title = productName;
            
            //信息为应用类型
            socialData.extConfig.wxMessageType = UMSocialWXMessageTypeOther; //设置为网页、音乐等其他类型
            
            WXWebpageObject *webObject = [WXWebpageObject object];    //初始化微信网页对象
            webObject.webpageUrl = url; //设置网页的url地址
            
            socialData.extConfig.wxMediaObject = webObject; //设置网页对象
            
        }
        else if ([platformName isEqualToString:UMShareToQQ])
        {
            
            //分享到QQ title+内容+图片+链接
            [UMSocialData defaultData].extConfig.qqData.title = [self optimizingShareTextLength:productName];
            
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qqData.shareText = productDescription;

        }
        else {
            
        }
    }
    
    //心情动态分享设置
    else
    {
        //心情动态图片
        NSArray *pictures = (NSArray *)_dynamic[@"pictures"];
        
        if (pictures != nil && pictures.count > 0) {
            socialData.shareImage = [Helper imageSuitable:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:pictures[0][@"previewUrl"]]]]]; //心情图片
            
        }
        else {
            socialData.shareImage = nil;
        }
        
        //分享到新浪微博和腾讯微博
        if ([platformName isEqualToString:UMShareToSina] || [platformName isEqualToString:UMShareToTencent] || [platformName isEqualToString:UMShareToQzone]) {

            //分享到新浪微博和腾讯微博 文本+图片
            //控制分享到微博的文本字数
            NSString *weiboShareText = nil;
            
            if ([[NSString stringWithFormat:@"%@ %@",url, _dynamic[@"content"]] length] > 140) {
                weiboShareText = [[NSString stringWithFormat:@"%@ %@",url,_dynamic[@"content"]] substringWithRange:NSMakeRange(0, 139)];
            }
            else{
                weiboShareText = [NSString stringWithFormat:@"%@ %@",url,_dynamic[@"content"]];
            }
            socialData.shareText = weiboShareText;
        }
        
        //分享到微信
        else if ([platformName isEqualToString:UMShareToWechatSession] || [platformName isEqualToString:UMShareToWechatTimeline]){
            
            //分享标题
            socialData.extConfig.title = _dynamic[@"content"];
            
            //信息为应用类型
            socialData.extConfig.wxMessageType = UMSocialWXMessageTypeOther; //设置为网页、音乐等其他类型
            
            WXWebpageObject *webObject = [WXWebpageObject object];    //初始化微信网页对象
            webObject.webpageUrl = url; //设置网页的url地址
            
            socialData.extConfig.wxMediaObject = webObject; //设置网页对象
        }
        
        else if ([platformName isEqualToString:UMShareToQQ]) {
            
            //分享到QQ title+内容+图片+链接
            [UMSocialData defaultData].extConfig.qqData.title = [self optimizingShareTextLength:_dynamic[@"content"]];
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qqData.shareText = [self optimizingShareTextLength:_dynamic[@"content"]];
            
        }
        else {
            
        }
    }
}

- (NSString *)optimizingShareTextLength:(NSString *)text
{
    if (!text) {
        return nil;
    }
    
    return [text length] > 128 ? [text substringWithRange:NSMakeRange(0, 127)]:text;
}

- (void)didFinishGetUMSocialDataResponse:(UMSocialResponseEntity *)response
{
    
}

- (void) dealloc
{
    _dynamic = nil;
}
@end
